package com.rongxun.main.factory;

import android.annotation.SuppressLint;
import android.app.Application;

import com.rongxun.main.model.AdvertisementModel;
import com.rongxun.main.viewmodel.AdvertisementViewModel;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;


/**
 * 创建者     forza
 * 创建时间   2019/11/29 18:09
 * 描述
 */
public class AdvertisementModelFactory extends ViewModelProvider.NewInstanceFactory {
    @SuppressLint("StaticFieldLeak")
    private static volatile AdvertisementModelFactory INSTANCE;
    private final           Application               mApplication;

    public static AdvertisementModelFactory getInstance(Application application) {
        if (INSTANCE == null) {
            synchronized (AdvertisementModelFactory.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AdvertisementModelFactory(application);
                }
            }
        }
        return INSTANCE;
    }
    private AdvertisementModelFactory(Application application) {
        this.mApplication = application;
    }
    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(AdvertisementViewModel.class)) {
            return (T) new AdvertisementViewModel(mApplication, new AdvertisementModel(mApplication));
        }
        throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
    }
}
